package com.dlc.beijingrfid.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.dlc.commonlibrary.dialog.WaitingDialog;
import com.dlc.commonlibrary.utils.KeyBoardUtils;
import com.dlc.commonlibrary.utils.PrefUtil;
import com.dlc.commonlibrary.utils.ToastUtil;
import com.dlc.beijingrfid.R;
import com.dlc.beijingrfid.base.BaseActivity;
import com.dlc.beijingrfid.base.Constants;
import com.dlc.beijingrfid.helper.UserHelper;
import com.dlc.beijingrfid.net.bean.LoginBean;
import com.dlc.beijingrfid.net.protocol.UserProto;
import com.trello.rxlifecycle.android.ActivityEvent;

import butterknife.BindView;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/7/18 0018.
 */

public class LoginActivity extends BaseActivity {


    @BindView(R.id.et_phone) EditText mEtPhone;
    @BindView(R.id.et_password) EditText mEtPassword;
    private WaitingDialog mDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        // TODO: 2017/7/24 测试数据
        //mEtPhone.setText("13018684890");
        //mEtPassword.setText("11111111");
        mEtPhone.setText(PrefUtil.getDefault().getString(Constants.LoginData.PHONE,""));
       // mEtPassword.setText(PrefUtil.getDefault().getString(Constants.LoginData.PW,""));
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_login;
    }

    public void forget_psd_tv(View view) {
        Intent intent =new Intent(this, ForgetPasswordActivity.class);
        intent.putExtra("tab",0);
        startActivity(intent);
    }

    public void login(View view) {
        valiData();
    }

    /**
     * 校验并登录
     */
    private void valiData() {

        final String username = mEtPhone.getText().toString();
        if (TextUtils.isEmpty(username)) {
            ToastUtil.show(this, R.string.please_input_username);
            mEtPhone.requestFocus();
            return;
        }
        final String password = mEtPassword.getText().toString();
        if (TextUtils.isEmpty(password)) {
            ToastUtil.show(this, R.string.please_input_password);
            mEtPassword.requestFocus();
            return;
        }

        login(username, password);
    }

    private void login(final String username, final String password) {
        KeyBoardUtils.closeKeybord(mEtPhone, this);
        KeyBoardUtils.closeKeybord(mEtPassword, this);

        showDialog();

        UserProto.get()
                .login(username, password, null)
                .subscribeOn(Schedulers.io())
                .compose(this.<LoginBean>bindUntilEvent(ActivityEvent.DESTROY))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<LoginBean>() {
                    @Override
                    public void onCompleted() {
                        dismissDialog();

                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissDialog();
                        showToast(e.getMessage());
                    }

                    @Override
                    public void onNext(LoginBean loginBean) {
                        PrefUtil.getDefault().edit().putString(Constants.LoginData.PHONE,username).apply();
                        PrefUtil.getDefault().edit().putString(Constants.LoginData.PW,password).apply();
                        UserHelper.get().saveUserInfo(loginBean);
                        if (loginBean.data.type == 3) {
                            startActivity(MainActivity.class);
                            finish();
                        }else {
                            showOneToast("账号不对");
                        }
                    }
                });
    }

    private void showDialog() {
        if (mDialog == null) {
            mDialog = WaitingDialog.newDialog(this).setMessage(R.string.login_ing);
            mDialog.setCancelable(false);
        }
        mDialog.show();
    }

    private void dismissDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }
}
